package com.example.lastwork;

import static java.lang.System.out;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class details extends AppCompatActivity {
    private String TAG="details";
    UserDao userDao = new UserDao();
    private int num;
    String a;
    String b;
    String id;
    String name1;

    List<Map<String,String>> data_list = new ArrayList<>();
    List<Map<String,String>> data_list2 = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);

        Intent intent = getIntent();
        Log.i(TAG,"num===="+intent.getStringExtra("num"));
        a =intent.getStringExtra("num");
        b =intent.getStringExtra("id");
        Log.i(TAG,"num===="+a);
        num = Integer.parseInt(a);
        ListView gridView = findViewById(R.id.d_list);
        TextView name = findViewById(R.id.d_name);
        TextView title = findViewById(R.id.d_title);
        TextView content = findViewById(R.id.d_content);
        Button bt = findViewById(R.id.d_love);
        Log.i(TAG,"num="+num);
        SharedPreferences sp = this.getSharedPreferences("rate", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        id = sp.getString("id","o");
        name1 = sp.getString("name","o");
        editor.apply();
        new Thread(){
            @Override
            public void run() {
                try {
                    userDao.getdetailData(data_list,num);
                    userDao.gettaolunData(data_list2,num);
                    String user = data_list.get(0).get("name");
                    String tit = data_list.get(0).get("title");
                    String cont = data_list.get(0).get("content");
                    String[] id2 = userDao.selectlover(id);
                    content.setText(cont);
                    Log.i(TAG, cont);
                    title.setText(tit);
                    Log.i(TAG, tit);
                    name.setText(user);
                    Log.i(TAG, user);
                    Log.i(TAG, user);
                    Log.i(TAG,"成功");
                    if(search(id2,data_list.get(0).get("id"))==-1){
                    }else{
                        bt.setText("已关注");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i(TAG, "失败");
                }
                details.this.runOnUiThread(new Runnable() {
                    public void run() {
                        String [] from ={"name","chat"};
                        int [] to = {R.id.f_id,R.id.f_name};
                        SimpleAdapter adapter = new SimpleAdapter(details.this, data_list2, R.layout.fivelistview, from, to);
                        gridView.setAdapter(adapter);
                        Log.i(TAG, "线程运行");
                    }
                });
                if(data_list==null){
                    out.println("该用户尚未发布文章");
                }

                Log.i(TAG, "线程运行");
            }

        }.start();
    }
    public void Onclick1(View view) throws Exception {
        switch(getTaskId())
        {
            case R.id.d_taolun:
                SharedPreferences sp = this.getSharedPreferences("rate", MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                id = sp.getString("id","o");
                name1 = sp.getString("name","o");
                editor.apply();
                EditText echat = findViewById(R.id.E_taolun);
                String chat = echat.getText().toString().trim();
                userDao.addtaolun(id,name1,chat,num);
            case R.id.d_love:
                userDao.addlover(id,b);

        }
    }

    private static int search(String[] aStr, String aIndex) {
        int index = -1;	// 将index的值初始化为-1
        if (aStr == null) {	// 如果str值为null，则抛出非法数据异常
            new IllegalArgumentException().printStackTrace();	// IllegalArgumentException非法数据异常
        } else {
            for (int i = 0; i < aStr.length; i++) {
                if (aIndex == aStr[i]) {	// 如果字符数组里面的字符和要搜索的字符一致
                    index = i;	// 就将i的值赋值给index
                    break;	// 满足条件，退出循环
                }
            }
        }
        return index;	// 打印index的值
    }
}